package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.RankHomeData;
import com.elinkway.infinitemovies.bean.RankItem;
import com.elinkway.infinitemovies.bean.RankList;
import com.elinkway.infinitemovies.utils.MoviesConstant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class RankHomeDataParser extends LVideoMobileParser<RankHomeData> {
	private static final String SORT = "sort";
	private static final String NAME = "name";
	private static final String THEMEID = "themeid";
	private static final String PIC="pic";
	private static final String PIC_M_HORSE = "pic_m_horse";
	private static final String VIDEOTYPE = "videotype";
	private static final String ALBUMID = "aid";
	private static final String SUBNAME = "subname";
	private static final String PLAYURL = "playurl";
	private static final String PLAYSITE = "playsite";
	private static final String SCORE = "score";
	
	private static final String ZONGYI = "m_rank_zongyi";
	private static final String MOVIE = "m_rank_movie";
	private static final String TV = "m_rank_tv";
	private static final String CARTOON = "m_rank_cartoon";
	
	private static final String DATA = "data";
	@Override
	public RankHomeData parse(JSONObject data) throws Exception {
		// TODO Auto-generated method stub
		RankHomeData homeData = new RankHomeData();
		data=data.getJSONObject("data");
		if(data.has(MOVIE)){
			homeData.setMovieRank(getChild(data.getJSONObject(MOVIE),MoviesConstant.VT_MOVIE));
		}
		if(data.has(TV)){
			homeData.setTvRank(getChild(data.getJSONObject(TV),MoviesConstant.VT_TV));
		}
		if(data.has(ZONGYI)){
			homeData.setZongyiRank(getChild(data.getJSONObject(ZONGYI),MoviesConstant.VT_ZONGYI));
		}
		if(data.has(CARTOON)){
			homeData.setCartoonRank(getChild(data.getJSONObject(CARTOON),MoviesConstant.VT_CARTOON));
		}
		return homeData;
	}
	private RankList getChild(JSONObject obj,String vt) throws JSONException{
		RankList children=new RankList();
		children.setVt(vt);
		children.setName(MoviesConstant.getVtName(vt));
		if (obj.has(DATA)){
			JSONArray arr = obj.getJSONArray(DATA);
			for (int i=0;i<arr.length();i++){
				RankItem childItem = new RankItem();
				JSONObject childObj = arr.getJSONObject(i);
				
				if (childObj.has(SORT)){
					childItem.setSort(childObj.getString(SORT));
				    }
				if (childObj.has(NAME)){
					childItem.setName(childObj.getString(NAME));
				    }
				if (childObj.has(THEMEID)){
					childItem.setThemeid(childObj.getString(THEMEID));
				    }
				if (childObj.has(PIC_M_HORSE)){
					childItem.setPic_m_horse(childObj.getString(PIC_M_HORSE));
				    }
				if (childObj.has(VIDEOTYPE)){
					childItem.setVt(childObj.getString(VIDEOTYPE));
				    }
				if (childObj.has(ALBUMID)){
					childItem.setAlbumid(childObj.getString(ALBUMID));
				    }
				if (childObj.has(SUBNAME)){
					childItem.setSubname(childObj.getString(SUBNAME));
				    }
				if (childObj.has(PLAYURL)){
					childItem.setPlayurl(childObj.getString(PLAYURL));
				    }
				if (childObj.has(PLAYSITE)){
					childItem.setPlaysite(childObj.getString(PLAYSITE));
				    }
				if (childObj.has(SCORE)){
					childItem.setRating(childObj.getString(SCORE));
				}
				if(childObj.has(PIC)){
					childItem.setPic(childObj.getString(PIC));
				}
				children.getRankList().add(childItem);
				// 排行榜首页固定5个item
				if(i==4){
					break;
				}
			}
		}
		return children;
		
		
	}
}

